package com.future.service;

import java.util.List;

import com.future.common.exception.AppException;
import com.future.dao.SysUserDao;
import com.future.model.SysUser;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;

import cn.hutool.crypto.SecureUtil;

public class SysUserService {
	@Inject
	private SysUser sysUser;
	@Inject
	private SysUserDao sysUserDao;

	public void validateUser(String username, String password, String type) {
		SysUser user = this.sysUserDao.findUserByUsername(username, type);
		if (user == null) {
			throw new AppException("不存在该用户!");
		}
		if (user.getSfsh().equals("0") || StrKit.isBlank(user.getSfsh())) {
			throw new AppException("该用户尚未通过审核！");
		}
		String passwordDb = user.getPassword();
		if (passwordDb == null || !passwordDb.equals(SecureUtil.md5(password))) {
			throw new AppException("密码错误!");
		}
	}

	public SysUser findUserById(String userid) {
		if (StrKit.isBlank(userid)) {
			throw new AppException("用户信息为空!");
		}
		return this.sysUser.findById(userid);
	}

	public List<SysUser> getUserList(String sfsh) {
		return this.sysUserDao.getUserList(sfsh);
	}

	public void passWordChange(String userId, String passwordold, String passwordnew) {
		SysUser sysUser = this.sysUser.findById(userId);
		if (sysUser == null) {
			throw new AppException("用户信息不存在!");
		}
		String passwordDB = sysUser.getPassword();
		if (!passwordDB.equals(SecureUtil.md5(passwordold))) {
			throw new AppException("旧密码输入错误！");
		}
		sysUser.setPassword(SecureUtil.md5(passwordnew));
		sysUser.update();
	}

	public void modifyUser(SysUser user) {
		user.update();
	}
}
